package com.reguang.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.reguang.bo.SysUserBO;
import com.reguang.common.utils.MD5Utils;
import com.reguang.pojo.SysRoleDO;
import com.reguang.pojo.SysUserDO;
import com.reguang.result.Result;
import com.reguang.service.SysRoleService;
import com.reguang.service.SysUserService;

@RequestMapping("/sys/user")
@Controller
public class SysUserController {

	private String prefix = "system/sysuser";

	@Autowired
	SysUserService sysUserService;

	@Autowired
	SysRoleService sysRoleService;

	@GetMapping("")
	String user(Model model) {
		return prefix + "/sysuser";
	}

	@GetMapping("/list")
	@ResponseBody
	PageInfo<SysUserDO> list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		PageInfo<SysUserDO> sysUserList = sysUserService.list(params);
		return sysUserList;
	}

	@GetMapping("/add")
	String add(Model model) {
		List<SysRoleDO> roles = sysRoleService.getAllRoles();
		model.addAttribute("roles", roles);
		return prefix + "/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") String id, Model model) {
		SysUserBO user = sysUserService.queryByPrimaryKey(id);
		model.addAttribute("user", user);
		List<SysRoleDO> roles = sysRoleService.queryListByUserId(id);
		model.addAttribute("roles", roles);
		return prefix + "/edit";
	}

	@ResponseBody
	@PostMapping("/save")
	public Result save(SysUserBO user) {
		user.setPassword(MD5Utils.encrypt(user.getNickName(), user.getPassword()));
		if (sysUserService.save(user) > 0) {
			return Result.success();
		}
		return Result.failure();
	}

	@ResponseBody
	@RequestMapping("/update")
	public Result update(SysUserBO user) {
		if (sysUserService.update(user) > 0) {
			return Result.success();
		}
		return Result.failure();
	}

	@PostMapping("/batchRemove")
	@ResponseBody
	Result batchRemove(@RequestParam(value = "ids[]") String[] ids) {
		if (sysUserService.batchRemove(ids) > 0) {
			return Result.success();
		}
		return Result.failure();
	}

	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String id) {
		if (sysUserService.deleteById(id) > 0) {
			return Result.success();
		}
		return Result.failure();
	}
	
	@GetMapping("/resetPwd/{id}")
	String resetPwd(@PathVariable("id") String userId, Model model) {
		SysUserDO userDO = new SysUserDO();
		userDO.setId(userId);
		model.addAttribute("user", userDO);
		return prefix + "/reset_pwd";
	}

	@PostMapping("/resetPwd")
	@ResponseBody
	Result resetPwd(SysUserDO user) {
		user.setPassword(
				MD5Utils.encrypt(sysUserService.queryByPrimaryKey(user.getId()).getNickName(), user.getPassword()));
		if (sysUserService.resetPwd(user) > 0) {
			return Result.success();
		}
		return Result.failure();
	}
}
